﻿using UnityEngine;
using System.Collections;
using System;


public class Botoncito : MonoBehaviour {
	public GameObject caja;
	public GameObject texto;
	public int idioma;
	public GameObject telon;
	DateTime fechita;
	public AudioClip titin;
	public AudioClip tambor;
	int cantidadMax=1;
	DateTime fechaAnterior;
	// Use this for initialization
	void Start () {
		if (Application.systemLanguage.ToString() == "Spanish") {
			idioma = 0;
		} else {
			idioma = 1;
		}
		//PlayerPrefs.DeleteAll ();
		fechita= System.DateTime.Now;
		//print (fechita);
		cantidadMax = PlayerPrefs.GetInt ("cantidad", 1);
		print (cantidadMax);
		fechaAnterior = DateTime.Parse(PlayerPrefs.GetString ("Fecha", "1990/11/21 12:30:00 PM"));
		print (fechaAnterior);
		print ((fechita - fechaAnterior).Minutes);
	

		if ((fechita - fechaAnterior).TotalHours < 1.0) {
			//pasaron menos de 24 hrs
						texto.SetActive (true);
						if (idioma == 1) {
								texto.GetComponent<UILabel> ().text = "Come back later for more random useless information";
						} 
						gameObject.SetActive (false);
				} else {//pasaron mas de 24 hrs
					PlayerPrefs.SetInt("cantidad",1);
					cantidadMax=1;
				}
		//print (Application.systemLanguage);

	}
	
	// Update is called once per frame
	void Update () {

	
	}
	public void clickBoton(){
		if (cantidadMax > 0) {
						GameObject.FindGameObjectWithTag ("MainCamera").audio.Play ();		
						StartCoroutine (click ());

				} else {
					texto.SetActive (true);
					if (idioma == 1) {
						texto.GetComponent<UILabel> ().text = "Come back later for more random useless information";
					} 
					gameObject.SetActive (false);
				}
	}
	IEnumerator click(){
		WWWForm form = new WWWForm ();
		form.AddField ("idioma", idioma);
		System.Random random = new System.Random ();
		double aux = random.NextDouble ();
		//print (aux);
		form.AddField ("random", aux.ToString());
		WWW download = new WWW ("http://www.cresta.cl/datoinutil/consulta.php",form);
		yield return download;

		if (!String.IsNullOrEmpty (download.error)) {
						GameObject.FindGameObjectWithTag ("MainCamera").audio.clip = titin;
						GameObject.FindGameObjectWithTag ("MainCamera").audio.Play ();		
						telon.GetComponent<TweenPosition> ().Toggle ();
						caja.SetActive (true);
						gameObject.GetComponent<BoxCollider> ().enabled = false;
						// informacion relacionada con error por no tener conexion a internet 
						if(idioma==0)
							caja.transform.FindChild ("Label").gameObject.GetComponent<UILabel> ().text = "Para recibir datos fantabulosamente inútiles requieres de internet. Si estás conectado porfavor espera mientras solucionamos el problema";
						else
							caja.transform.FindChild ("Label").gameObject.GetComponent<UILabel> ().text = "Please connect to the internet. If you are connected, please wait until we fix our server problems.";
				} else {

						//if (download.text != "error") {
			
								GameObject.FindGameObjectWithTag ("MainCamera").audio.clip = titin;
								GameObject.FindGameObjectWithTag ("MainCamera").audio.Play ();		
								telon.GetComponent<TweenPosition> ().Toggle ();
								caja.SetActive (true);
								gameObject.GetComponent<BoxCollider> ().enabled = false;

								caja.transform.FindChild ("Label").gameObject.GetComponent<UILabel> ().text = download.text;
								cantidadMax--;
								print (cantidadMax);
								PlayerPrefs.SetInt ("cantidad", cantidadMax);
								PlayerPrefs.SetString ("Fecha", System.DateTime.Now.ToString ());
						//}
				}


	}
	public void OKclick(){
		PlayerPrefs.Save ();
		GameObject.FindGameObjectWithTag ("MainCamera").audio.clip = tambor;
		caja.SetActive (false);
		if (cantidadMax > 0) {
			//puedo seguir	
			gameObject.GetComponent<BoxCollider>().enabled=true;
			
		} else {
			texto.SetActive (true);
			if (idioma == 1) {
				texto.GetComponent<UILabel> ().text = "Come back later for more random useless information";
			} 
			gameObject.SetActive (false);
		}
		print ("fin ok");
		//gameObject.GetComponent<BoxCollider>().enabled=true;
		//gameObject.SetActive (false);
	}

}
